#conding:utf-8
import wx
import copy

class MyGauge(wx.PyWindow):
    def __init__(self, parent, id=wx.ID_ANY, range=(-200,200),minvalue = -100, maxvalue = 100,
                pos = wx.DefaultPosition, size=(400,60), style=0):

        wx.PyWindow.__init__(self,parent,id,pos,size,style)

        self._size = size
        
        self._border_colour = None
        self._barColour    = [wx.Colour(220,0,0), wx.GREEN, wx.RED]
        
        self._border_padding = 0
        self._range = range
        self._value = 0.00
        self._min = minvalue
        self._max = maxvalue
        self._font = None
        
        


        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
              
        
    def DoGetBestSize(self):
        return wx.Size(self._size[0], self._size[1])

    def SetCursorFont(self, font):
        self._font = font

    def GetBorderColour(self):
        return self._boder_colour

    def SetBorderColour(self, colour):
        self._border_colour = colour

    SetBoderColor = SetBorderColour
    GetBoderColor = GetBorderColour

    def GetBarColour(self, index):
        return self._barColour[index]

    def SetBarColour(self, index, color):
        self._barColour[index] = color

    SetBarColor = SetBarColour
    GetBarColor = GetBarColour

    def GetBoderPadding(self):
        return self._border_padding

    def SetBorderPadding(self, padding):
        self._border_padding = padding

    def GetRange(self):
        return self._range

    def SetRange(self,range):
        if type(range) != type(()):
            raise Exception("ERROR:\n Gauge range must be tuple")
        self._range = range
        self.Refresh()

    def GetValue(self):
        return self._value

    def SetValue(self, value):
        self._value = value
        self.Refresh()

    def SetMinValue(self,value):
        self._min = value
        self.Refresh()

    def SetMaxValue(self,value):
        self._max = value
        self.Refresh()

    def OnSize(self, event):
        self.Refresh()
        event.Skip()
        
    def OnEraseBackground(self, event):
        pass

    def OnPaint(self, event):
        try:
            dc = wx.BufferedPaintDC(self)
            rect = self.GetClientRect()
         
            dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
            dc.Clear()
            colour = self.GetBackgroundColour()
            dc.SetBrush(wx.Brush(colour))
            dc.SetPen(wx.Pen(colour))
            dc.DrawRectangleRect(rect)
            
    
    
            if self._border_colour:
                dc.SetPen(wx.Pen(self.GetBorderColour()))
                dc.DrawRectangleRect(rect)
                pad = 1 + self.GetBorderPadding()
                rect.Deflate(pad,pad)
    
            dc.SetPen(wx.Pen(wx.Colour(10,100,150),1))
            r = copy.copy(rect)
            r.x = 0
            r.y = 0
            r.width = rect.width
            r.height = rect.height-15 
            dc.DrawRectangleRect(r)
    
            m_count = float(r.width)/(self._range[1]-self._range[0])
            
            dc.SetPen(wx.Pen(wx.BLACK, 3,wx.DOT))
            
            if(self._min > self._max):
                self._min, self._max = self._max, self._min
            if (self._min < self._range[0]):
                self._min = self._range[0] - 5
            if (self._max > self._range[1]):
                self._max = self._range[1] + 5
            
            l_v = self._min - self._range[0]
            #dc.DrawLine(l_v*m_count , 0, l_v*m_count , rect.height-15)# left start line condition
            dc.DrawLabel('%.2f' % float(self._min), wx.Rect(l_v*m_count-15,rect.height-15,30,10))
            
            r_v = self._max - self._range[0]             
            #dc.DrawLine(r_v*m_count, 0, r_v*m_count, rect.height-15)#right end line(max) condition
            dc.DrawLabel('%.2f' % float(self._max), wx.Rect(r_v*m_count-15,rect.height-15,30,10))
    
            mid = (self._max - self._min)/2 + self._min
            mid_v = mid - self._range[0] 
            
            dc.SetPen(wx.Pen(wx.BLACK,5))
            dc.SetBrush(wx.Brush(self._barColour[0]))
           # dc.DrawLine(mid_v*m_count, 0, mid_v*m_count, rect.height-15)  #mid
            dc.DrawLabel('%.2f' % float(mid),wx.Rect(mid_v*m_count-15, rect.height-15, 30, 10))
            
            #print l_v,r_v,mid,m_count
            #print self._min,self._max,self._value,self._range[0],self._range[1]
            #print self._value
            
            
            #value = self._value * m_count
            m_font = wx.Font(35,74,90,90,False,wx.EmptyString)
            if self._value < mid:
                if self._value >= self._min and self._value < mid:
                    color = self._barColour[1]
                    dc.SetBrush(wx.Brush(color))
                    dc.SetPen(wx.Pen(color))
                    r.x = (self._value - self._range[0])*m_count
                    r.y = 0
                    r.width = (mid - self._value)*m_count
                    r.height = rect.height-15
                    dc.DrawRectangleRect(r)
    
                    dc.SetBrush(wx.Brush(wx.BLACK))
                    dc.SetPen(wx.Pen(wx.BLACK))
                    lq_font = wx.Font(35,74,90,90,False,wx.EmptyString)
                    lq_font.SetPixelSize(wx.Size(r.height/3,r.height))
                    dc.SetFont(lq_font)
                    dc.DrawLabel('%.2f' % float(self._value), wx.Rect((mid-self._range[0])*m_count - r.height*4/5,0, 100, rect.height))
                    dc.SetPen(wx.Pen(wx.BLACK,3,wx.DOT))
                    #dc.DrawLine(mid, 0, mid, rect.height-15)
                elif self._value < self._min:
                    #print self._value, (self._min*m_count)
                    #print '222'
                    color = self._barColour[0]
                    dc.SetBrush(wx.Brush(color))
                    dc.SetPen(wx.Pen(color))
                    r.x = (self._value-self._range[0])*m_count
                    r.y = 0
                    r.width = (mid - self._value)*m_count
                    r.height = rect.height-15
                    dc.DrawRectangleRect(r)
                    
                    dc.SetBrush(wx.Brush(wx.BLACK))
                    dc.SetPen(wx.Pen(wx.BLACK))
                    lnq_font = wx.Font(35,74,90,90,False,wx.EmptyString)
                    lnq_font.SetPixelSize(wx.Size(r.height/3,r.height))
                    dc.SetFont(lnq_font)
                    dc.DrawLabel('%.2f' % float(self._value), wx.Rect((mid-self._range[0])*m_count - r.height*4/5, 0, 100, rect.height))
                    dc.SetPen(wx.Pen(wx.BLACK,3,wx.DOT))
                    #dc.DrawLine(l_v*m_count , 0, l_v*m_count , rect.height-15)                                     
            elif self._value > mid:
                if self._value <= self._max and self._value > mid:
                    color = self._barColour[1]
                    dc.SetBrush(wx.Brush(color))
                    dc.SetPen(wx.Pen(color))
                    r.x = (mid - self._range[0])*m_count
                    #print r.x
                    r.y = 0
                    r.width = (self._value - mid)*m_count
                    r.height = rect.height-15
                    dc.DrawRectangleRect(r)
    
                    dc.SetBrush(wx.Brush(wx.BLACK))
                    dc.SetPen(wx.Pen(wx.BLACK))
                    rq_font = wx.Font(35,74,90,90,False,wx.EmptyString)
                    rq_font.SetPixelSize(wx.Size(r.height/3,r.height))
                    dc.SetFont(rq_font)
                    dc.DrawLabel('%.2f' % float(self._value), wx.Rect((mid-self._range[0])*m_count - r.height*4/5,0, 100, rect.height))
                    dc.SetPen(wx.Pen(wx.BLACK,3,wx.DOT))
                    #dc.DrawLine(mid, 0, mid, rect.height-15)
                    
                elif self._value > self._max:
                    #print '444'
                    color = self._barColour[0]
                    dc.SetBrush(wx.Brush(color))
                    dc.SetPen(wx.Pen(color))
                    r.x = (mid-self._range[0])*m_count
                    r.y = 0
                    r.width = (self._value - mid)*m_count
                    r.height = rect.height-15
                    dc.DrawRectangleRect(r)
                    
                    dc.SetBrush(wx.Brush(wx.BLACK))
                    dc.SetPen(wx.Pen(wx.BLACK))
                    rnq_font = wx.Font(35,74,90,90,False,wx.EmptyString)
                    rnq_font.SetPixelSize(wx.Size(r.height/3,r.height))
                    dc.SetFont(rnq_font)
                    dc.DrawLabel('%.2f' % float(self._value), wx.Rect((mid-self._range[0])*m_count - r.height*4/5,0, 100, rect.height))
                    #dc.DrawLabel('%.2f' % float((self._value-mid)/m_count), wx.Rect((mid-self._range[0])*m_count-50, 0, 100, rect.height))
                    dc.SetPen(wx.Pen(wx.BLACK,3,wx.DOT))
                    #dc.DrawLine(r_v*m_count, 0, r_v*m_count, rect.height-15)             
    
            elif self._value == mid:
                #print '555'
                dc.SetPen(wx.Pen(wx.GREEN,3))
                dc.DrawLine((self._value-self._range[0])*m_count, 0, (self._value-self._range[0])*m_count, rect.height-15)
                dc.SetPen(wx.Pen(wx.BLACK,3))
                
                #m_font = wx.Font(35,74,90,90,False,wx.EmptyString)
                m_font.SetPixelSize(wx.Size(r.height/3,r.height))
                sz = m_font.GetPixelSize()
                print sz.GetWidth()
                
                #dc.SetFont(wx.Font( r.height, 74, 90, 90, False, wx.EmptyString ))
                dc.SetFont(m_font)
                dc.DrawLabel('%.2f' % float(self._value), wx.Rect((self._value-self._range[0])*m_count - r.height*4/5, 0, 100, rect.height))
    
            dc.SetPen(wx.Pen(wx.BLACK,3))
            dc.SetBrush(wx.Brush(self._barColour[0]))
            dc.DrawLine(l_v*m_count , 0, l_v*m_count , rect.height-15)# left start line condition
            #dc.DrawLabel('%.2f' % float(self._min), wx.Rect(l_v*m_count-15,rect.height-15,30,10))
            
            dc.DrawLine(r_v*m_count, 0, r_v*m_count, rect.height-15)#right end line(max) condition
            #dc.DrawLabel('%.2f' % float(self._max), wx.Rect(r_v*m_count-15,rect.height-15,30,10))
    
            dc.SetPen(wx.Pen(wx.BLACK,3,wx.DOT))
            dc.DrawLine(mid_v*m_count, 0, mid_v*m_count, rect.height-15)  #mid
            #dc.DrawLabel('%.2f' % float(mid),wx.Rect(mid_v*m_count-15, rect.height-15, 30, 10))
        except:
            pass

class MainFrame(wx.Frame):
    def __init__(self,parent):
        wx.Frame.__init__(self,parent,wx.ID_ANY,u'MyGauge Test',
                          wx.DefaultPosition,wx.DefaultSize,wx.DEFAULT_FRAME_STYLE)

        bSizer = wx.BoxSizer(wx.VERTICAL)
        gauge = MyGauge(self)
        bSizer.Add(gauge,1,wx.EXPAND,1)

        self.SetSizer(bSizer)
        self.Layout()

if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame(None)
    frame.Show()
    app.MainLoop()
    
